package model;

import game_engine.GameEngine;
import game_engine.core.GameStage;
import game_engine.core.Vector2f;
import game_engine.display.GEUtil;
import game_engine.menu.BasicMenuItem;
import game_engine.menu.MenuItem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.TrueTypeFont;

public class StartMenu implements GameStage {
	
	private TrueTypeFont font;
	private BasicMenuItem startGameMenuItem;
	private BasicMenuItem optionMenuItem;
	private BasicMenuItem exitMenuItem;
	private boolean running = false;
	
	private final long keyDelay = 100;
	private long lastKeyTyped = 0;
	
	private MenuItem[] menuItems;
	private int currentMenu = 0;
	
	public StartMenu()  {
		GameEngine ge = GameEngine.getInstance();
		ge.addFontToLoad("courier18", "Courier new", 18);
		while(!ge.isAllFontLoaded()) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
		this.font = ge.getFont("courier18");
		float startX = GEUtil.getCenterFromScreen(100, 100).x;
		float startY = GEUtil.getCenterFromScreen(150, 100).y - 50;
		float optionX = startX;
		float optionY = startY + 50 ;
		float exitX = startX;
		float exitY = startY + 100;
		
		this.startGameMenuItem = new BasicMenuItem("Start game", new Vector2f(startX, startY), font);
		this.startGameMenuItem.setSelected(true);
		this.startGameMenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				StartMenu.this.stop();
				LevelOne levelOne = new LevelOne();
				GameEngine.getInstance().setGameStage(levelOne);
				levelOne.start();
			}
		});
		
		this.optionMenuItem = new BasicMenuItem("Option", new Vector2f(optionX, optionY), font);
		
		this.exitMenuItem = new BasicMenuItem("Exit", new Vector2f(exitX, exitY), font);
		this.exitMenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameEngine.getInstance().stop();
				
			}
		});
		
		menuItems = new MenuItem[]{this.startGameMenuItem, this.optionMenuItem, this.exitMenuItem};
	}
	

	@Override
	public void render() {
		this.startGameMenuItem.render();
		this.optionMenuItem.render();
		this.exitMenuItem.render();
	}

	@Override
	public void start() {
		this.running = true;
	}

	@Override
	public void stop() {
		this.running = false;
	}

	@Override
	public boolean isRunning() {
		return running;
	}

	@Override
	public void update(long tick) {
		keyUodate();
	}
	
	private void keyUodate() {
		if((System.currentTimeMillis() - lastKeyTyped)>keyDelay) {
			if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)) {
				menuItems[currentMenu].actionPerformed();
			}
			if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
				switchMenu(true);
			}
			if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
				switchMenu(false);
			}
			lastKeyTyped = System.currentTimeMillis();
		}
	}
	
	private void switchMenu(boolean down) {
		menuItems[currentMenu].setSelected(false);
		if(down)
			currentMenu = (currentMenu+1)%menuItems.length;
		else
			currentMenu = (currentMenu + menuItems.length - 1)%menuItems.length;
		menuItems[currentMenu].setSelected(true);
	}

}
